package com.szqhyxj.youxiangju.biz.card.cardApply;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.annotations.NonNull;

/**
 * Created by leung on 2018/11/1.
 */

public class CardApplyActivity extends BaseActivity implements CardApplyContract.View, NavigationBar.NavigationBarListener {

    CardApplyPresenter presenter;
    @BindView(R.id.applycardList)
    RecyclerView applycardList;
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;


    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, CardApplyActivity.class);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_apply);
        ButterKnife.bind(this);
        presenter = new CardApplyPresenter(this);
        presenter.getCardList();
        navigationBar.setNavigationBarListener(this);
    }

    @Override
    public void showList(List<CardApplyModel.CardApplyEntity> list) {
        applycardList.setLayoutManager(new GridLayoutManager(this, 3));
        RecyclerviewAdapter adapter = new RecyclerviewAdapter(this, list);
        applycardList.setAdapter(adapter);

    }

    @Override
    public void showFail(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.ViewHolder> {

        private Context context;
        private List<CardApplyModel.CardApplyEntity> data;

        public RecyclerviewAdapter(Context context, List<CardApplyModel.CardApplyEntity> data) {
            this.context = context;
            this.data = data;

        }

        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.list_apply_card_item_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
            holder.cardname.setText(data.get(position).getBankname());
            Glide.with(CardApplyActivity.this).load(data.get(position).getImgUrl()).into(holder.cardIcon);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //     Log.e("这里是点击每一行item的响应事件",""+position+item);
                    /*Intent intent = new Intent();
                    intent.setData(Uri.parse(data.get(position).getWebUrl()));//Url 就是你要打开的网址
                    intent.setAction(Intent.ACTION_VIEW);
                    startActivity(intent); //启动浏览器*/

                    //GoWebActivity.start(CardApplyActivity.this,data.get(position).getWebUrl());
                    WebActivity.start(CardApplyActivity.this,"申请信用卡",data.get(position).getWebUrl());

                }
            });

        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private TextView cardname;
            private ImageView cardIcon;

            public ViewHolder(View itemView) {
                super(itemView);
                cardname = itemView.findViewById(R.id.cardname);
                cardIcon = itemView.findViewById(R.id.cardicon);


            }
        }
    }
}
